package com.zrrd.yunchmall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.product.entity.Product;
import com.zrrd.yunchmall.product.service.IProductService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2024-01-15
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/product")
@Api(tags = "商品服务-商品管理模块")
public class ProductController {
    @Autowired
    private IProductService productService;

    /*
    返回商品信息
     */
    @ApiOperation("查询商品详情")
    @GetMapping("/{id}")
    public ResponseResult detail(@PathVariable Integer id) {
        Product product = productService.getById(id);
        if (product != null)
            return new ResponseResult(200, "查询成功", product);
        else
            return new ResponseResult(200, "该商品不存在");
    }
    /*
    修改库存的方法
     */
    @PostMapping("/subStock")
    public ResponseResult subStock(@RequestParam("pid") Integer pid, @RequestParam("num") Integer num) {
        Product product = productService.getById(pid);
        product.setStock(product.getStock() - num);//减少库存
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("stock", product.getStock());//只修改库存
        updateWrapper.eq("id", product.getId());//pid做条件
        productService.update(updateWrapper);
        return new ResponseResult(200, "修改库存成功");
    }

    @ApiOperation("退货/关闭订单释放内存")
    @PostMapping("/stock/free")
    public ResponseResult freeStock(@RequestBody List<Map<String, Long>> params) {
        productService.freeStock(params);
        return new ResponseResult(200, "执行成功");
    }

    @ApiOperation("查询商品列表")
    @GetMapping("/list")
    @Cacheable(value = "product", key = "#root.methodName+'_'+#pageNum+'_'+#pageSize")
    public ResponseResult list(@RequestParam(required = false) @ApiParam("商品名称关键字") String keyword,
                               @RequestParam(defaultValue = "1") int pageNum,
                               @RequestParam(defaultValue = "5") int pageSize,
                               @RequestParam(required = false) @ApiParam("上架状态") Integer publishStatus,
                               @RequestParam(required = false) @ApiParam("审核状态") Integer verifyStatus,
                               @RequestParam(required = false) @ApiParam("商品编码") String productSn,
                               @RequestParam(required = false) @ApiParam("商品类别ID") Integer productCategoryId,
                               @RequestParam(required = false) @ApiParam("品牌ID") Integer brandId) {
        /*QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(keyword))
            queryWrapper.like("name", keyword);
        if (publishStatus != null)
            queryWrapper.eq("publish_status", publishStatus);
        if (verifyStatus != null)
            queryWrapper.eq("verify_status", verifyStatus);
        if (!StringUtils.isEmpty(productSn))
            queryWrapper.eq("product_sn", productSn);
        if (productCategoryId != null)
            queryWrapper.eq("product_category_id", productCategoryId);
        if (brandId != null)
            queryWrapper.eq("brand_id", brandId);
        return new ResponseResult(200, "查询成功",
                productService.page(new Page<>(pageNum, pageSize), queryWrapper));*/
        return new ResponseResult(200, "查询成功",
                productService.page(keyword, publishStatus, verifyStatus, productSn, productCategoryId, brandId, pageNum, pageSize));
    }

    @ApiOperation("删除商品")
    @PostMapping("/update/deleteStatus")
    public ResponseResult updateDeleteStatus(@RequestParam int deleteStatus,
                                             @RequestParam String ids) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        // ids = 1,2,3
        // update pms_product set delete_status=? where id IN (1, 2, 3)
        updateWrapper.set("delete_status", deleteStatus);
        updateWrapper.in("id", ids.split(","));
        productService.update(updateWrapper);
        return new ResponseResult(200, "修改成功");
    }

    @ApiOperation("新增商品信息")
    @PostMapping("/create")
    public ResponseResult create(@RequestBody Product product) {
        productService.save(product);
        return new ResponseResult(200, "添加成功");
    }

    @GetMapping("/updateInfo/{id}")
    public ResponseResult updateInfo(@PathVariable Integer id) {
        return new ResponseResult(200, "查询成功", productService.detail(id));
    }

    @GetMapping("/testSleuth")
    public String testSleuth() {
        return "商品：测试链路追踪";
    }

}
